package jt.classic.system.workflow.impl;

import jt.classic.system.workflow.WObject;
import jt.classic.system.workflow.WPlug;
import jt.classic.system.workflow.WPlugHandler;
import jt.classic.system.workflow.WorkflowException;

import org.limp.basework.AbstractSimpleBean;

import org.limp.mine.StringTool;

import java.util.HashMap;
import java.util.Map;


/**
 * 插件接口实现类
 * @author bizkit
 *
 */
public class WPlugImpl extends AbstractSimpleBean implements WPlug {
    private final static String DEFAULT_HANDLER = "jt.classic.system.workflow.impl.DefaultWPlugHandler";
    private WObject obj;
    private int type = 1;
    private String handleName;
    private String sid;

    public WPlugHandler ihandler() throws WorkflowException {
        // TODO Auto-generated method stub
        WPlugHandler handler = null;

        try {
            if (StringTool.checkString(this.handleName).equals("")) {
                handler = (WPlugHandler) Class.forName(DEFAULT_HANDLER)
                                              .newInstance();
            } else {
                handler = (WPlugHandler) Class.forName(this.handleName.trim())
                                              .newInstance();
            }

            Map temp = new HashMap();

            temp.put("WPLUG", this);

            handler.reload(temp);
        } catch (Exception ex) {
            throw new WorkflowException("get plug handler error.", ex);
        }

        return handler;
    }

    public String isid() {
        // TODO Auto-generated method stub
        return this.sid;
    }

    public int itype() {
        return this.type;
    }

    public WObject iobject() {
        return this.obj;
    }

    public void reload(Map info) {
        if (info != null) {
            this.info = info;

            this.sid = (String) this.info.get("SID");
            this.type = this.info.containsKey("TYPE")
                ? Integer.parseInt((String) this.info.get("TYPE")) : 1;
            this.handleName = (String) this.info.get("HANDLER");
            this.obj = (WObject) this.info.get("WOBJECT");
        }
    }
}
